package com.edu.servlet;

import com.edu.bean.User;
import com.edu.service.UserPostService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/career/reply")
public class UserCareerReplyServlet extends HttpServlet {
    private UserPostService postService = new UserPostService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 检查登录状态
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"success\":false,\"message\":\"请先登录\"}");
            return;
        }

        // 获取参数
        String postId = request.getParameter("postId");
        String content = request.getParameter("content");

        // 参数验证
        if (postId == null || postId.trim().isEmpty() || content == null || content.trim().isEmpty()) {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"success\":false,\"message\":\"参数错误\"}");
            return;
        }

        try {
            // 添加回复
            boolean success = postService.userAddReply(Integer.parseInt(postId), user.getUserId(), content.trim());

            // 返回结果
            response.setContentType("application/json;charset=utf-8");
            if (success) {
                response.getWriter().write("{\"success\":true,\"message\":\"回复成功\"}");
            } else {
                response.getWriter().write("{\"success\":false,\"message\":\"回复失败\"}");
            }
        } catch (NumberFormatException e) {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"success\":false,\"message\":\"参数错误\"}");
        }
    }
}